package cn.vc.auth.service;

import cn.vc.auth.mapper.RelationMapper;
import cn.vc.auth.entity.po.Relation;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RelationService {
    private final RelationMapper relationMapper;

    @Autowired
    public RelationService(RelationMapper relationMapper) {
        this.relationMapper = relationMapper;
    }


    /**
     * <h2>新增用户关系</h2>
     * @param userId1
     * @param userId2
     * @return 返回1为新关注，返回2为双向关注，0为已关注，-1为关注失败
     */
    public int insert(Long userId1, Long userId2) {
        // 不能关注自己
        if (userId1.equals(userId2)) return -1;
        Relation relation = relationMapper.selectByUserId1AndUserId2(userId1, userId2);
        // 用户关系不存在
        if (relation==null) {
            relation = new Relation(userId1, userId2);
            return relationMapper.insert(relation)==1?1:-1;
        } else {
            // 已删除
            if (relation.getDeleted().equals(1)) {
                // 修改为未删除
                relation.setDeleted(0);
                // 不为我关注的
                if (!(relation.getUserId1().equals(userId1) && relation.getUserId2().equals(userId2))) {
                    // 调换关注者和被关注者的ID
                    long UserId1 = relation.getUserId2();
                    relation.setUserId1(relation.getUserId2());
                    relation.setUserId2(UserId1);
                }
                // 修改
                return relationMapper.myUpdateById(relation)==1?1:-1;
            } else {
                // 为我关注的
                if (relation.getUserId1().equals(userId1) && relation.getUserId2().equals(userId2)) {
                    // 已关注
                    return 0;
                } else {
                    // 对方已关注且未删除
                    // 为双向关注，即已关注，返回0
                    if (relation.isBilateral()) return 0;
                    // 未双向关注，修改未双向关注，返回2
                    else {
                        relation.setBilateral(true);
                        // 修改
                        return relationMapper.updateById(relation)==1?2:-1;
                    }
                }
            }
        }
    }


    /**
     * <h2>取消关注用户</h2>
     * @param userId1
     * @param userId2
     * @return
     */
    public boolean delete(Long userId1, Long userId2) {
        Relation relation = relationMapper.selectByUserId1AndUserId2(userId1, userId2);
        // 用户关系为空
        if (relation==null) {
            return false;
        }
        // 用户关系为双向，改为单向
        else if (relation.isBilateral()) {
            relation.setUserId1(userId2);
            relation.setUserId2(userId1);
            relation.setBilateral(false);
            System.out.println("修改");
            return this.updateById(relation);
        }
        // 用户关系为单向，且为己方关注，才可以取关
        else if (relation.getUserId1().equals(userId1) && relation.getUserId2().equals(userId2)) {
            System.out.println("删除");
            return relationMapper.deleteById(relation.getId())==1;
        }
        // 其他原因，取关失败
        else {
            return false;
        }
    }



    /**
     * 查询指定ID的好友
     * @param id
     * @return
     */
    public List<Relation> selectOnFriend(Long id) {
        LambdaQueryWrapper<Relation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Relation::getUserId1, id).eq(Relation::isBilateral, 1).eq(Relation::getDeleted, 0)
                .or().eq(Relation::getUserId2, id).eq(Relation::isBilateral, 1).eq(Relation::getDeleted, 0);
        return relationMapper.selectList(lqw);
    }
    /**
     * 查询指定ID的关注
     * @param userId1
     * @return
     */
    public List<Relation> selectByUserId1(Long userId1) {
        LambdaQueryWrapper<Relation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Relation::getUserId1, userId1).eq(Relation::getDeleted, 0).eq(Relation::isBilateral, false);
//                .or().eq(Relation::getUserId2, userId1).eq(Relation::isBilateral, 0).eq(Relation::getDeleted, 0);
        return relationMapper.selectList(lqw);
    }
    /**
     * 查询指定ID的粉丝
     * @param userId2
     * @return
     */
    public List<Relation> selectByUserId2(Long userId2) {
        LambdaQueryWrapper<Relation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Relation::getUserId2, userId2).eq(Relation::getDeleted, 0).eq(Relation::isBilateral, false);
        return relationMapper.selectList(lqw);
    }
    /**
     * 查询指定ID的是否关注某个ID
     * @param userId1
     * @param userId2
     * @return
     */
    public Relation selectIsAttention(Long userId1, Long userId2) {
        LambdaQueryWrapper<Relation> lqw = new LambdaQueryWrapper<>();
        // 我关注他 或者 他关注我并且为双向（互相关注）
        lqw.eq(Relation::getUserId1, userId1).eq(Relation::getUserId2, userId2)
                .or()
                .eq(Relation::getUserId1, userId2).eq(Relation::getUserId2, userId1).eq(Relation::isBilateral, true);
        return relationMapper.selectOne(lqw);
    }



    public boolean updateById(Relation relation) {
        return relationMapper.updateById(relation)==1;
    }
}
